package com.hitcoder.chat.utils

import android.app.Dialog
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.hitcoder.chat.R

/**
 * 录音对话框管理工具类
 */
class DialogManager(private val mContext: Context) {
    //弹出对话框
    private var dialog: Dialog? = null

    //录音图标
    private var icon: ImageView? = null

    //音量显示 图标
    private var voice: ImageView? = null

    //对话框上提示文字
    private var label: TextView? = null

    /**
     * 显示对话框
     */
    fun showRecordingDialog() {
        //根据指定sytle实例化Dialog
        dialog = Dialog(mContext, R.style.AudioDialog)
        val view = LayoutInflater.from(mContext).inflate(R.layout.dialog_recorder, null)
        icon = view.findViewById(R.id.id_recorder_dialog_icon)
        voice = view.findViewById(R.id.id_recorder_dialog_voice)
        label = view.findViewById(R.id.id_recorder_dialog_label)
        dialog?.setContentView(view)
        dialog?.setCancelable(false)
        dialog?.show()
    }

    fun overTime(s: String) {
        label?.text =
            String.format(mContext.resources.getString(R.string.str_recorder_countdown), s)
    }

    /**
     * 正在录音状态的对话框
     */
    fun recording() {
        if (dialog != null && dialog!!.isShowing) {
            icon?.visibility = View.VISIBLE
            voice?.visibility = View.VISIBLE
            label?.visibility = View.VISIBLE
            icon?.setImageResource(R.drawable.recorder)
            label?.setText(R.string.str_recorder_up_cancel)
        }
    }

    /**
     * 取消录音状态对话框
     */
    fun wantToCancel() {
        if (dialog != null && dialog!!.isShowing) {
            icon?.visibility = View.VISIBLE
            voice?.visibility = View.GONE
            label?.visibility = View.VISIBLE
            icon?.setImageResource(R.drawable.cancel)
            label?.setText(R.string.str_recorder_want_cancel)
        }
    }

    /**
     * 时间过短提示的对话框
     */
    fun tooShort() {
        if (dialog != null && dialog!!.isShowing) { //显示状态
            icon?.visibility = View.VISIBLE
            voice?.visibility = View.GONE
            label?.visibility = View.VISIBLE
            icon?.setImageResource(R.drawable.voice_to_short)
            label?.setText(R.string.str_recorder_too_short)
        }
    }

    /**
     * 取消（关闭）对话框
     */
    fun dismissDialog() {
        if (dialog != null && dialog!!.isShowing) { //显示状态
            dialog?.dismiss()
            dialog = null
        }
    }

    /**
     * 显示更新音量级别的对话框
     */
    fun updateVoiceLevel(level: Int) {
        if (dialog != null && dialog!!.isShowing) { //显示状态
            icon?.visibility = View.VISIBLE
            //            mVoice.setVisibility(View.VISIBLE);
            label?.visibility = View.VISIBLE
            //设置图片的id，我们放在drawable中的声音图片是以v+数字格式的
            val resId =
                mContext.resources.getIdentifier("v$level", "drawable", mContext.packageName)
            voice?.apply {
                if (visibility == View.VISIBLE) setImageResource(resId)
            }
        }
    }
}